package org.logtime.blog.service;

import java.util.Collections;
import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * @version: 1.0
 * @Author: itmins
 * @Email: itmins@gmail.com
 */
@Service("cacheService")
public class CacheService {

	public void clear() {
		this.getCache().clear();
	}

	public void clear(String key) {
		this.getCache().remove(key);
	}

	public Object get(String key) {
		return this.getCache().get(key);
	}

	public void put(String key, Object value) {
		this.getCache().put(key, value);
	}

	public Cache getCache() {
		try {
			Cache cache = CacheManager.getInstance().getCache("cache");
			if (null == cache) {
				cache = CacheManager.getInstance().getCacheFactory()
						.createCache(Collections.emptyMap());
				CacheManager.getInstance().registerCache("cache", cache);
			}
			return cache;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}